%Code to randomise placement of some object and then to detect the
%placement of it through MLE
%Author: Nathan Rich
clear
clc
cla

x=randi(10); %Randomise Placement of object in km
y=randi(10);
a1 = 0; %Position of Antenna relative to x axis, 0 on y axis
a2 = 1;

del_ret = 0.01; %delay return will be 0.01 s/km
dis_a1 = sqrt((x - a1)^2 + (y - 0)^2); %distance from object to antenna 1
del_a1 = mod(dis_a1*del_ret*100,2*pi); %Phase delay in a1 to object and back, *100 to get integer

dis_a2 = sqrt((x - a2)^2 + (y - 0)^2);
del_a2 = mod(dis_a2*del_ret*100,2*pi);

%Both antenna first transmit a cosine then wait to receive it back from
%object.

for I = 1:400
    noise1 = 0;%((-1)^randi(2))*rand()/4;
    R1(I) = noise1 + cos((I*100 - del_a1)); %Received Signal 1
    S(I) = cos(I*100); %Sent Signal
    dc(I) = 10;
    Time(I) = I;
end

for I = 1:400
    noise2 = 0;%((-1)^randi(2))*rand()/4;
    R2(I) = noise2 + cos((I*100 - del_a2));%Received Signal 2
end

%With ideal LPF 
d  = fdesign.lowpass('Fp,Fst,Ap,Ast',0.1,0.12,0.1,1e-4,'linear');
Hd = design(d, 'equiripple');
Out = filter(Hd.Numerator,1,S.*R1);
figure(1)
plot(Time,Out)
title('Filtered Output')
xlabel('Time (ms)')
ylabel('Amplitude')

figure(2)
plot(R1, 'b')
hold on
plot(R2, 'r')
hold on
plot(S, 'g')
title('Recieved signals vs original')
xlabel('Time (ms)')
ylabel('Amplitude')
legend('Received Signal 1', 'Received Signal 2', 'Sent Signal')
